package cdut.manytomany;
import org.hibernate.Session;
import org.junit.Test;
import utils.HibernateUtils;
public class ManyToManyTest {

    @Test
    public void test1(){
        Session session = HibernateUtils.getSession();
        session.beginTransaction();
        //创建两个学生
        Student s1=new Student();
        s1.setSname("李哥");
        Student s2 = new Student();
        s2.setSname("折木一一");
        //创建两个科目
        Course c1 = new Course();
        c1.setCname("java");
        Course c2 = new Course();
        c2.setCname("Web");
        //建立关联关系

        s1.getCourse().add(c1);
        s2.getCourse().add(c1);
        s1.getCourse().add(c2);
        s2.getCourse().add(c2);

        //存储
        session.save(c1);
        session.save(c2);
        session.save(s1);
        session.save(s2);

        //提交事务，关闭session
        session.getTransaction().commit();;
        session.close();
    }
}
